﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;

namespace Student_management
{
    public partial class frmBangDiemMon : Form
    {
        Connection db = new Connection();
        public frmBangDiemMon()
        {
            InitializeComponent();
        }

        private void frmBangDiemMon_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
            if (frmMain.quyen == "admin")
            {
                cboMon.DataSource = db.getDS("select * from Mon");
                cboMon.DisplayMember = "TENMON";
                cboMon.ValueMember = "MAMON";
            }
            else
            {
                cboMon.DataSource = db.getDS("select * from Mon where MaMon='" + frmMain.mon + "'");
                cboMon.DisplayMember = "TENMON";
                cboMon.ValueMember = "MAMON";
            }
            cboLop.DataSource = db.getDS("select * from lop");
            cboLop.DisplayMember = "MaLop";
            cboLop.ValueMember = "MALOP";
            cboHocky.SelectedIndex = 0;
            loaddata();
        }

        void loaddata()
        {
            DataTable a;
            a = db.getDS("select MaHS[Mã Học Sinh], HoTen[Họ Và Tên] from HocSinh where MaLop = '" + cboLop.Text + "'");
            a.Columns.Add("Điểm 15p");
            a.Columns.Add("Điểm 1 tiết");
            a.Columns.Add("Điểm học kì");
            a.Columns.Add("Điểm trung bình");
            a.Columns.Add("STT");            
            grdt.DataSource = a;
            grdt.Columns[0].ReadOnly = true;
            grdt.Columns[1].ReadOnly = true;
            grdt.Columns[6].ReadOnly = true;
            grdt.Columns[5].ReadOnly = true;
            grdt.Columns["STT"].DisplayIndex = 0;

            for (int i = 0; i < a.Rows.Count; i++)
            {
                a.Rows[i]["STT"] = i + 1;
                Diem c = LayDiem(grdt.Rows[i].Cells[0].Value.ToString(), cboHocky.Text, cboMon.SelectedValue.ToString());
                grdt.Rows[i].Cells[2].Value = c.D15P;
                grdt.Rows[i].Cells[3].Value = c.D1Tiet;
                grdt.Rows[i].Cells[4].Value = c.DHK;
                grdt.Rows[i].Cells[5].Value = c.DTB;
            }
        }
        Diem LayDiem(string MaHS, string MaHK, string MaMon)
        {
            Diem a = new Diem();
            DataTable b = new DataTable();
            b = db.getDS("select D15P, D1Tiet, DHK from CTDiem where MaHS = '" + MaHS + "' and MaHK = '" + MaHK + "' and MaMon = '" + MaMon + "'");
            if (b.Rows.Count > 0)
            {
                a.D15P = b.Rows[0][0].ToString().Replace(",", ".");
                a.D1Tiet = b.Rows[0][1].ToString().Replace(",", ".");
                a.DHK = b.Rows[0][2].ToString().Replace(",", ".");
                a.TinhDiem();
            }
            return a;
        }

        string checknull(string a)
        {
            if (a == null)
                return "null";
            else
                return "'" + a + "'";
        }

        private Diem GetData(string MaHS, string D15p, string D1Tiet, string DHK)
        {
            Diem hs = new Diem();
            hs.MaHS = MaHS;
            hs.MaMon = cboMon.SelectedValue.ToString();
            hs.MaHK = cboHocky.Text;
            if (D15p != "")
                hs.D15P = D15p.Replace(",",".");
            if (D1Tiet != "")
                hs.D1Tiet = D1Tiet.Replace(",", ".");
            if (DHK != "")
                hs.DHK = DHK.Replace(",", ".");
            hs.TinhDiem();
            return hs;
        }

        private void cboHocky_SelectedIndexChanged(object sender, EventArgs e)
        {
            loaddata();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < grdt.RowCount; i++)
                {
                    Diem hs = GetData(grdt.Rows[i].Cells[0].Value.ToString(), grdt.Rows[i].Cells[2].Value.ToString(), grdt.Rows[i].Cells[3].Value.ToString(), grdt.Rows[i].Cells[4].Value.ToString());
                    if (checkdiem(hs.D15P) || checkdiem(hs.D1Tiet) || checkdiem(hs.DHK))
                    {
                        MessageBox.Show("Điểm phải từ 0 đến 10", "Lỗi");
                        return;
                    }
                    if (db.getDS("Select * from CTDiem where MaHS = '" + hs.MaHS + "' and MaMon = '" + hs.MaMon + "' and MaHK = '" + hs.MaHK + "'").Rows.Count > 0)
                        db.getDS("Update CTDiem set D15P=" + checknull(hs.D15P) + ",D1Tiet=" + checknull(hs.D1Tiet) + ",DHK=" + checknull(hs.DHK) + ",DTB=" + checknull(hs.DTB) + " where MaHS='" + hs.MaHS + "' and MaMon='" + hs.MaMon + "' and MaHK='" + hs.MaHK + "'");
                    else
                        db.getDS("Insert into CTDiem (MaHS, MaMon, MaHK, D15P, D1Tiet, DHK, DTB) values ('" + hs.MaHS + "','" + hs.MaMon + "','" + hs.MaHK + "'," + checknull(hs.D15P) + "," + checknull(hs.D1Tiet) + "," + checknull(hs.DHK) + "," + checknull(hs.DTB) + ")");
                }
                MessageBox.Show("Lưu thành công");
                loaddata();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Xem Lai Du Lieu Da Nhap");
            }
        }

        bool checkdiem(string a)
        {
            if (a != null)
                return float.Parse(a, CultureInfo.InvariantCulture) > 10 || float.Parse(a, CultureInfo.InvariantCulture) < 0;
            else
                return false;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            loaddata();
        }
    }
}
